package com.ruoyi.es.service.impl;

import com.ruoyi.es.common.ResultInfo;
import com.ruoyi.es.entily.Video;
import com.ruoyi.es.service.EsVideoService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class EsVideoServiceImpl implements EsVideoService {

    @Resource
    RestHighLevelClient restHighLevelClient;

    @Override
    public ResultInfo findByVideo(Video video) throws IOException {
        SearchRequest searchRequest = new SearchRequest("esvideo");
        searchRequest.types("class_schedule");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 高亮 字段
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("content");
        //前置标签
        highlightBuilder.preTags("<font style='color:red'>");
        //后置标签
        highlightBuilder.postTags("</font>");
        searchSourceBuilder.highlighter(highlightBuilder);

        //模糊查询、区间查询
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (!StringUtils.isEmpty(video.getVideo_content())){
            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("content", video.getVideo_content());
            boolQueryBuilder.must(matchQueryBuilder);
        }


        searchSourceBuilder.query(boolQueryBuilder);
        //根据价格倒叙排序
        searchSourceBuilder.sort("coursePrice", SortOrder.DESC);
        searchRequest.source(searchSourceBuilder);

        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        //获取 总页数
        long total = search.getHits().getTotalHits();

        SearchHit[] hits = search.getHits().getHits();
        ArrayList<Object> list = new ArrayList<>();
        for (SearchHit hit : hits) {
            //获取ID
            String id = hit.getId();
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //插入ID
            sourceAsMap.put("id",id);
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            if (highlightFields.size()>0){
                //替换高亮字段
                HighlightField highlightField = highlightFields.get("className");
                String className = highlightField.getFragments()[0].toString();
                sourceAsMap.put("className",className);
            }
            list.add(sourceAsMap);
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total",total);

        return new ResultInfo(true,2000,"查询成功",map);
    }
}
